package com.e1xp.deport_management_system.controller;


import com.e1xp.deport_management_system.entity.User;
import com.e1xp.deport_management_system.entity.Vendors;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.exception.CustomExceptionType;
import com.e1xp.deport_management_system.service.IVendorsService;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
@Controller
@RequestMapping("/vendors")
public class VendorsController {

    @Resource
    IVendorsService vendorsService;

    /**
     * 用于渲染供货商页面
     *
     * @param instanceId 实例id
     * @return ModelAndView
     */
    @RequestMapping("/vendorsData")
    public ModelAndView vendorsData(@SessionAttribute long instanceId, @SessionAttribute("user") User adminUser) {
        ModelAndView modelAndView = new ModelAndView();
        if (adminUser.getType() != 0) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            List<Vendors> vendorsList = vendorsService.getList(instanceId);
            modelAndView.addObject("vendorsList", vendorsList);
            modelAndView.setViewName("vendorsManager");
        }
        return modelAndView;
    }

    /**
     * 用于渲染添加供应商页面
     *
     * @return ModelAndView
     */
    @RequestMapping("/addVendorsPage")
    public ModelAndView addPage(@SessionAttribute("user") User adminUser) {
        ModelAndView modelAndView = new ModelAndView();
        if (adminUser.getType() != 0) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.setViewName("addVendors");
        }
        return modelAndView;
    }

    /**
     * 添加供应商
     *
     * @param instanceId 实例id
     * @param vendors    供应商实体
     * @return AjaxResponse
     * @throws CustomException 当供应商信息不完整，已有同名供应商，操作用户非管理员时抛出该异常
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResponse add(@SessionAttribute long instanceId, @SessionAttribute("user") User adminUser, Vendors vendors) throws CustomException {
        if (adminUser.getType() != 0) {
            //添加用户非管理员
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "非管理员无添加供应商权限");
        }
        vendorsService.add(instanceId, vendors);
        return AjaxResponse.success();
    }

    /**
     * 用于更新供应商
     *
     * @param instanceId 实例id
     * @param vendors    供应商实体
     * @return AjaxResponse
     * @throws CustomException 当 实例不存在，供应商信息不完整，操作用户非管理员 时抛出该异常
     */
    @RequestMapping("/update")
    @ResponseBody
    public AjaxResponse update(@SessionAttribute long instanceId, @SessionAttribute("user") User adminUser, Vendors vendors) throws CustomException {
        if (adminUser.getType() != 0) {
            //添加用户非管理员
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "非管理员无添加用户权限");
        }
        vendorsService.update(instanceId, vendors);
        return AjaxResponse.success();
    }

    /**
     * 渲染详情页面
     *
     * @param instanceId 实例id
     * @param vendorsId  供应商id
     * @return ModelAndView
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@SessionAttribute long instanceId, long vendorsId) {
        ModelAndView modelAndView = new ModelAndView();
        Vendors vendors = vendorsService.get(instanceId, vendorsId);
        if (vendors == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.addObject("vendors", vendors);
            modelAndView.setViewName("changeVendors");
        }
        return modelAndView;
    }

    /**
     * 用于获取供应商实体
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PostMapping("getSelection")
    @ResponseBody
    public AjaxResponse getSelection(@SessionAttribute long instanceId) {
        List<Map<String, String>> selectList = vendorsService.getSelection(instanceId);
        return AjaxResponse.success(selectList);
    }
}
